package top.hitotright.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class SecondarySortDriver extends Configured implements Tool {
	
	
	public static void main(String[] args) throws Exception {
		if (args.length < 2) {
			throw new IllegalArgumentException("args:<input-dir><output-dir>");
		}
		
		int status = ToolRunner.run(new SecondarySortDriver(), args);
		System.exit(status);
	}

	

	public int run(String[] args) throws Exception {
		// TODO 自动生成的方法存根
		Configuration conf = getConf();
		Job job = new Job(conf);
		job.setJobName("SecondarySortDriver");
		job.setJarByClass(SecondarySortDriver.class);
		FileInputFormat.setInputPaths(job,new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		
		//第一步：映射器，读取数据并处理
		job.setMapperClass(SecondarySortMapper.class);
		//第二步：分区器，对映射器输出的数据进行分组，保证相同自然健的数据所有数据都发送给同一个归约器
		job.setPartitionerClass(DateTemperaturePartitioner.class);
		//第三步，分组比较器，数据到底归约器之前进行排序分组
		job.setGroupingComparatorClass(DateTemperatureGroupingComparator.class);
		//第四步，归约器，输出数据
		job.setReducerClass(SecondarySortReducer.class);
		
		boolean status = job.waitForCompletion(true);
		return status ? 0 : 1;
	}
}
